package com.easyadmin.controller;

import com.easyadmin.annotation.GlobalInterceptor;
import com.easyadmin.annotation.VerifyParam;
import com.easyadmin.entity.enums.*;
import com.easyadmin.entity.po.SysAccount;
import com.easyadmin.entity.query.SysAccountQuery;
import com.easyadmin.entity.vo.AjaxResponseVO;
import com.easyadmin.entity.vo.PaginationResult;
import com.easyadmin.exception.BusinessException;
import com.easyadmin.service.SysAccountService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/api/settings")
public class AccountController extends ABaseController {

    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Resource
    private SysAccountService sysAccountService;

    /**
     * @Description: 账号列表
     * @auther: 多多洛
     * @date: 10:40 2020/9/29
     * @param: [accountQuery]
     * @return: AjaxResponseVO
     */
    @RequestMapping("loadAccountList")
    @GlobalInterceptor(permissionCode = PermissionCodeEnum.SETTINGS_ACCOUNT)
    public AjaxResponseVO loadAccountList(SysAccountQuery accountQuery) {
        try {
            accountQuery.setOrderBy("create_time desc");
            accountQuery.setQueryRoles(true);
            PaginationResult<SysAccount> roles = sysAccountService.findListByPage(accountQuery);
            return getSuccessResponseVO(roles);
        } catch (Exception e) {
            logger.error("获取账号信息信息异常", e);
            return getServerErrorResponseVO();
        }
    }

    /**
     * @Description: 保存账号
     * @auther: 多多洛
     * @date: 13:57 2020/11/21
     * @param: [sysAccount]
     * @return: AjaxResponseVO
     */
    @RequestMapping("saveAccount")
    @GlobalInterceptor(permissionCode = PermissionCodeEnum.SETTINGS_ACCOUNT_EDIT, opObject = OperModuleEnum.OBJECT_ACCOUNT, opDescription = "保存账号，参数信息:#{#param0}")
    public AjaxResponseVO saveAccount(@VerifyParam SysAccount sysAccount) {
        try {
            this.sysAccountService.saveSysAccount(sysAccount);
            return getSuccessResponseVO(null);
        } catch (BusinessException e) {
            logger.error("保存账号信息信息异常", e);
            return getBusinessErrorResponseVO(e);
        } catch (Exception e) {
            logger.error("保存账号信息信息异常", e);
            return getServerErrorResponseVO();
        }
    }

    /**
     * @Description: 删除账号
     * @auther: 多多洛
     * @date: 16:51 2020/9/29
     * @param: [userId]
     * @return: AjaxResponseVO
     */
    @RequestMapping("delAccount")
    @GlobalInterceptor(permissionCode = PermissionCodeEnum.SETTINGS_ACCOUNT_DEL, opObject = OperModuleEnum.OBJECT_ACCOUNT, opDescription = "删除账号，账号ID:#{#param0}")
    public AjaxResponseVO delAccount(@VerifyParam(required = true) Integer userId) {
        try {
            this.sysAccountService.deleteByUserId(userId);
            return getSuccessResponseVO(null);
        } catch (Exception e) {
            logger.error("删除账号信息信息异常", e);
            return getServerErrorResponseVO();
        }
    }

    /**
     * @Description: 修改密码
     * @auther: 多多洛
     * @date: 16:51 2020/9/29
     * @param: [userId, password]
     * @return: AjaxResponseVO
     */
    @RequestMapping("updatePassword")
    @GlobalInterceptor(permissionCode = PermissionCodeEnum.SETTINGS_ACCOUNT_UPDATE_PASSWORD, opObject = OperModuleEnum.OBJECT_ACCOUNT, opDescription = "修改账号密码，账号ID" +
            ":#{#param0}")
    public AjaxResponseVO updatePassword(@VerifyParam(required = true) Integer userId, @VerifyParam(required = true, regex = VerifyRegexEnum.PASSWORD) String password) {
        try {
            this.sysAccountService.updatePassword(userId, null, password);
            return getSuccessResponseVO(null);
        } catch (Exception e) {
            logger.error("修改账号密码异常", e);
            return getServerErrorResponseVO();
        }
    }

    /**
     * @Description: 更新状态
     * @auther: 多多洛
     * @date: 13:57 2020/11/21
     * @param: [userId, status]
     * @return: AjaxResponseVO
     */
    @RequestMapping("updateStatus")
    @GlobalInterceptor(permissionCode = PermissionCodeEnum.SETTINGS_ACCOUNT_OP_STATUS, opObject = OperModuleEnum.OBJECT_ACCOUNT, opDescription = "#{#param1==0" +
            "?'禁用':'启用'}账号，账号ID:#{#param0}")
    public AjaxResponseVO updateStatus(@VerifyParam(required = true) Integer userId, @VerifyParam(required = true) Integer status) {
        try {
            UserStatusEnum statusEnum = UserStatusEnum.getByStatus(status);
            if (statusEnum == null) {
                throw new BusinessException(ResponseCodeEnum.CODE_600);
            }
            SysAccount sysAccount = new SysAccount();
            sysAccount.setStatus(status);
            this.sysAccountService.updateByUserId(sysAccount, userId);
            return getSuccessResponseVO(null);
        } catch (BusinessException e) {
            logger.error("修改账号状态异常", e);
            return getBusinessErrorResponseVO(e);
        } catch (Exception e) {
            logger.error("修改账号状态异常", e);
            return getServerErrorResponseVO();
        }
    }
}
